﻿using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.Utilities;
using WorseGame.Common.Configs;
using WorseGame.Content.Prefixes;

namespace WorseGame.Common.GlobalItems
{
    public class WorseItem : GlobalItem
    {
        public override bool InstancePerEntity => true;
        protected override bool CloneNewInstances => true;
        public float MinScale = 0.5f;
        public float MaxScale = 1.8f;
        public override bool PreDrawTooltipLine(Item item, DrawableTooltipLine line, ref int yOffset)
        {
            if (ItemConfig.Instance.ChaosTooltip)
            {
                Vector2 position = new(line.X, line.Y);
                float maxHeight = 0f;
                for (int i = 0; i < line.Text.Length; i++)
                {
                    line.BaseScale = new Vector2(Main.rand.NextFloat(MinScale, MaxScale), Main.rand.NextFloat(MinScale, MaxScale));
                    string charStr = line.Text[i].ToString();
                    Vector2 charSize = line.Font.MeasureString(charStr) * line.BaseScale;
                    Utils.DrawBorderStringFourWay(Main.spriteBatch, line.Font, charStr, position.X + charSize.X / 2, position.Y + charSize.Y / 2, line.Color, Color.Black, new Vector2(0.3f), 1);
                    position.X += charSize.X;
                    maxHeight = Math.Max(maxHeight, charSize.Y);
                }
                return false;
            }
            return base.PreDrawTooltipLine(item, line, ref yOffset);
        }
        public override void SetDefaults(Item item)
        {
            if (ItemConfig.Instance.AutoReuse)
            {
                item.autoReuse = false;
            }
            if (ItemConfig.Instance.DamageDefault)
            {
                item.DamageType = DamageClass.Default;
            }
            if (ItemConfig.Instance.DoubleMP)
            {
                item.mana *= 2;
            }
            if (ItemConfig.Instance.MaxStack)
            {
                item.maxStack = 1;
            }
            if (ItemConfig.Instance.RandomResearch)
            {
                if (item.ResearchUnlockCount > 0 && item.type < ItemID.Count && !Main.LocalPlayer.HasItem(item.type))
                {
                    int multiplier = Main.rand.Next(1, 201);
                    item.ResearchUnlockCount *= multiplier;
                }
            }
            if (ItemConfig.Instance.FishingPowerHalved)
            {
                item.fishingPole /= 2;
            }
            if (ItemConfig.Instance.BaitPowerHalved)
            {
                item.bait /= 2;
            }
            if (ItemConfig.Instance.CritChance)
            {
                item.crit = -114514;
            }
        }
        public override int ChoosePrefix(Item item, UnifiedRandom rand)
        {
            if (ItemConfig.Instance.WorstPrefix)
            {
                if (item.DamageType == DamageClass.Melee || item.DamageType == DamageClass.MeleeNoSpeed || item.DamageType == DamageClass.SummonMeleeSpeed)
                {
                    return ModContent.PrefixType<ScrapIron>();
                }
                if (item.DamageType == DamageClass.Ranged)
                {
                    return ModContent.PrefixType<Rusty>();
                }
                if (item.DamageType == DamageClass.Magic || item.DamageType == DamageClass.Summon)
                {
                    return ModContent.PrefixType<Volatile>();
                }
            }
            return base.ChoosePrefix(item, rand);
        }        
    }
}
